<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ title }}企业</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm1" :model="addForm" :rules="addFormRules" label-width="100px">
            <el-form-item label="企业名称:" prop="name">
              <el-input v-model="addForm.name" placeholder="请输入企业名称" size="mini" />
            </el-form-item>
            <el-form-item label="法人:" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" placeholder="请输入法人" size="mini" />
            </el-form-item>
            <el-form-item label="注册地址:" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" placeholder="请输入注册地址" size="mini" />
            </el-form-item>
            <el-form-item label="所在行业:" prop="industryCode">
              <el-select v-model="addForm.industryCode" style="width:100%" size="mini">
                <el-option
                  v-for="item in lineData"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                  placeholder="请选择所在行业"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="营业执照" prop="businessLicenseUrl">
              <template>
                <el-upload
                  action="#"
                  :file-list="fileList"
                  :before-upload="beforeUpload"
                  :http-request="uploadRequest"
                  :before-remove="removeRequest"
                >
                  <el-button v-if="!isSuccess" size="small" type="primary">点击上传</el-button>
                  <el-button v-else size="small" type="success" plain disabled>已上传</el-button>
                  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过 5MB</div>
                </el-upload>
                <!-- <img v-else :src="addForm.businessLicenseUrl" alt="" style="width:150px"> -->

              </template>

            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm2" :model="addForm" :rules="addFormRules" label-width="100px">
            <el-form-item label="企业联系人:" prop="contact">
              <el-input v-model="addForm.contact" size="mini" placeholder="请输入企业联系人" />
            </el-form-item>
            <el-form-item label="联系电话:" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" size="mini" placeholder="请输入联系电话" />
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button :disabled="isEdit" @click="onReset">重置</el-button>
        <el-button type="primary" @click="confirmSubmit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addFirmApi, alertFirmApi, getFirmDetailApi, getLineApi, uploadApi } from '@/api/firm'
import { phoneValidate } from '@/utils/validate'

export default {
  data() {
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      id: '',
      lineData: [], // 行业列表
      fileList: [], // 营业执照列表
      addFormRules: {
        name: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' }
        ], // 企业名称
        legalPerson: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' }
        ], // 法人
        registeredAddress: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' }
        ], // 注册地址
        industryCode: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' }
        ], // 所在行业
        contact: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' }
        ], // 企业联系人
        contactNumber: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' },
          { validator: phoneValidate, trigger: 'blur' }
        ], // 联系人电话
        businessLicenseUrl: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' }
        ], // 营业执照url
        businessLicenseId: [
          { required: true, message: '必填项，内容不能为空', trigger: 'blur' }
        ] // 营业执照id
      },
      isSuccess: false,
      isEdit: false
    }
  },
  computed: {
    title() {
      if (this.$route.params.id) {
        return '编辑'
      }
      return '添加'
    }
  },
  async created() {
    if (this.$route.params.id) {
      this.id = this.$route.params.id
      const res = await getFirmDetailApi(this.id)
      // //
      // console.log(res)
      this.addForm = res
      this.isSuccess = true
      this.fileList.push({
        name: res.businessLicenseName,
        url: res.businessLicenseUrl
      })
      this.isEdit = true
    }
    const res = await getLineApi()
    this.lineData = res
  },
  methods: {
    onReset() {
      this.$refs.ruleForm1.resetFields()
      this.$refs.ruleForm2.resetFields()
    },
    async confirmSubmit() {
      this.$refs.ruleForm1.validate()
      this.$refs.ruleForm2.validate()
      if (this.id) {
        const { businessLicenseId, businessLicenseUrl, contact, contactNumber, industryCode, legalPerson, name, registeredAddress } = this.addForm

        await alertFirmApi({ id: this.id, businessLicenseId, businessLicenseUrl, contact, contactNumber, industryCode, legalPerson, name, registeredAddress })
        this.$message.success('修改成功')
      } else {
        await addFirmApi(this.addForm)
        this.$message.success('添加成功')
      }
      this.$router.back()
    },
    beforeUpload(file) {
      const allowType = ['image/jpeg', 'image/png', 'image/jpg'].includes(file.type)
      const is5M = file.size / 1024 / 1024 < 5
      if (!allowType) {
        this.$message.error('上传营业执照图片只能是 PNG/JPG/JPEG 格式!')
      }
      if (!is5M) {
        this.$message.error('上传营业执照图片大小不能超过 5MB!')
      }
      return allowType && is5M
    },
    async uploadRequest(event) {
      console.log(event.file.size)
      const file = event.file
      if (event.file.size) {
        const formData = new FormData()
        formData.append('file', file)
        formData.append('type', 'businessLicense')
        const res = await uploadApi(formData)
        this.addForm.businessLicenseUrl = res.url
        this.addForm.businessLicenseId = res.id
        this.isSuccess = true
        console.log(this.addForm)
      }
    },
    removeRequest(file) {
      this.isSuccess = false
    }
  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 50px;
        line-height: 50px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 0 65px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
